package com.kpl.kplstzble.utils;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import java.util.ArrayList;
import java.util.Vector;

public class JschUtil {
    public static ArrayList<String> getFolders0() {
        String host = "39.107.104.228";
        String user = "root";
        String password = "shi135328";

        JSch jsch = new JSch();
        Session session;
        ArrayList<String> folders=new ArrayList<>();

        {
            try {
                session = jsch.getSession(user, host, 10022);
                session.setPassword(password);
                session.setConfig("StrictHostKeyChecking", "no");
                session.connect();

                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp sftpChannel = (ChannelSftp) channel;

                String remotePath = "/www/server/nginx/html/download/stzfile1";

                Vector<ChannelSftp.LsEntry> files = sftpChannel.ls(remotePath);
                for (ChannelSftp.LsEntry file : files) {
                    if (file.getAttrs().isDir()) {
                        if (!file.getFilename().equals(".") && !file.getFilename().equals("..")) {
                            folders.add(file.getFilename());
                        }
                    }
                }
                sftpChannel.exit();
                session.disconnect();
            } catch (JSchException | SftpException e) {
                e.printStackTrace();
            }
            System.out.println(folders);
        }
            return folders;
    }
    public static ArrayList<String> getFolders1(String folder) {
        String host = "39.107.104.228";
        String user = "root";
        String password = "shi135328";

        JSch jsch = new JSch();
        Session session;
        ArrayList<String> folders=new ArrayList<>();

        {
            try {
                session = jsch.getSession(user, host, 10022);
                session.setPassword(password);
                session.setConfig("StrictHostKeyChecking", "no");
                session.connect();

                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp sftpChannel = (ChannelSftp) channel;

                String remotePath = "/www/server/nginx/html/download/stzfile1/"+folder+"/";

                Vector<ChannelSftp.LsEntry> files = sftpChannel.ls(remotePath);
                for (ChannelSftp.LsEntry file : files) {
                    if (file.getAttrs().isDir()) {
                        if (!file.getFilename().equals(".") && !file.getFilename().equals("..")) {
                            folders.add(file.getFilename());
                        }
                    }
                }
                sftpChannel.exit();
                session.disconnect();
            } catch (JSchException | SftpException e) {
                e.printStackTrace();
            }
            System.out.println(folders);
        }
        return folders;
    }
}
